#include "Debug.h"
#include "Path.h"
#define _CRTDBG_MAP_ALLOC 
#include <stdlib.h>
#include <crtdbg.h>

Path::Path(int startIn, int endIn)
{
	start = startIn;
	end = endIn;
}


Path::~Path(void)
{
}

void Path::getEdges(set<EdgeBase*>& out, map<int,EdgeBase*> edges, map<int,VertexBase*> verts)
{
	vector<int> routeDuplicate = route;
	routeDuplicate.insert(routeDuplicate.begin(), start);
	routeDuplicate.push_back(end);

	for(int i=0; i<(routeDuplicate.size()-1); i++)	
	{
		int edge = getEdgeIndex( verts[ routeDuplicate[i] ], verts[ routeDuplicate[i+1] ]);
		if( edge == -1)
			return;
		out.insert( edges[ edge ] );	
	}
}

int Path::getEdgeIndex(VertexBase* a, VertexBase* b)
{
	vector<int> aSet;
	a->getConnectedEdges(aSet);
	vector<int> bSet;
	b->getConnectedEdges(bSet);
	
	for(int i=0; i<aSet.size(); i++)
		for(int j=0; j<bSet.size(); j++)
			if(aSet[i] == bSet[j])
				return aSet[i];
	return -1;
}

void Path::getVertices(set<VertexBase*>& out, map<int,VertexBase*> verts)
{
	out.insert(verts[start]);
	for(int i=0; i<route.size(); i++)
		out.insert(verts[route[i]]);
	out.insert(verts[end]);
}

void Path::add(Path* input)
{	
	if(input->size() == 0)
		return;
	for(int i=0; i< input->size() ; i++)
		route.push_back(input->getRoute()[i]);
}

void Path::add(int input)
{
	route.push_back(input);
}

int Path::getStart()
{
	return start;
}

int Path::getEnd()
{
	return end;
}

int Path::size()
{
	return (int)route.size();
}

vector<int> Path::getRoute()
{
	return route;
}

void Path::print()
{
	cout << "Route from " << start << " to " << end << " is: " ;
	if(route.size() == 0 )
		cout << "direct" << endl;
	else	
		for(int i=0; i<route.size(); i++)
			cout << " " << route[i] << ",";	
	cout << "\t" << length << endl;
}

void Path::setLength(double l)
{
	length = l;
}

double Path::getLength() const
{
	return length;
}